const textToType = {
	'护照':5,
	'身份证':1,
	'港澳居民来往内地通行证':6,
	'台湾居民来往大陆通行证':7,
	'新版外国人永久居留身份证':11,
	'其他证件':4
}

const typeToText = {
	1:'身份证',
	4:'其他证件',
	5:'护照',
	6:'港澳居民来往内地通行证',
	7:'台湾居民来往大陆通行证',
	11:'新版外国人永久居留身份证'
}

export const setPersonType = (data,type=1) => {//type:1 船票, 2 景点
	if(!Array.isArray(data)) return;
	let nowParse = Date.parse(new Date().Format('yyyy-MM-dd'));
	data.forEach(d=>{
		let _age = d.age||dealAge(d,nowParse);
		if(type==1){
			if(_age<=6) d.personType = 3;
			else if(_age>6&&_age<=14) d.personType = 2;
			else if(_age>14&&_age<60) d.personType = 1;
			else if(_age>=60) d.personType = 1;
		}else if(type==2){
			if(_age<=6) d.personType = 3;
			else if(_age>6&&_age<18) d.personType = 2;
			else if(_age>=18&&_age<60) d.personType = 1;
			else if(_age>=60) d.personType = 4;
		}
	})
	return data;
}

// 计算年龄(周岁)
const dealAge=(d,nowParse)=>{
	let _age = '';
	if(d.cardType==1){
		let idcardNyr = d.card.substr(6,8);
		let nyr = idcardNyr.substr(0,4)+'-'+idcardNyr.substr(4,2)+'-'+idcardNyr.substr(6,2);
		if(d.length==15){
			idcardNyr = d.substr(6,6);
			nyr = '19'+idcardNyr.substr(0,2)+'-'+idcardNyr.substr(2,2)+'-'+idcardNyr.substr(4,2);
		}
		let idcardParse = Date.parse(nyr);
		let diffYear = Math.ceil((nowParse-idcardParse)/86400/365/1000);
		_age = diffYear - 1;
	}
	return _age
}

export default {
	textToType,
	typeToText,
	setPersonType
};